// pages/myComments/myComments.js
var app = getApp()
var util = require("../../utils/util.js")
var getComment_url = "https://www.hkzhaoscut.top/normalUser/getCommentByUser"
var delete_url = "https://www.hkzhaoscut.top/normalUser/deleteCommentByUser"
var pageNum = 2
var pageSize = 8
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName: '', //用户名
    userToken: '', //用户token
    commentList: [], //评论列表
    totalPage: '', //总页数
  },
  // 删除评论
  deleteComment(e) {
    console.log(e)
    var commentId = e.currentTarget.dataset.commentid
    var that = this
    wx.showModal({
      title: '删除',
      content: '确定删除该评论吗？',
      success: function(res) {
        if (res.confirm) {
          wx.request({
            url: delete_url,
            data: {
              Comment_id: commentId,
              user_token: that.data.userToken
            },
            success(res) {
              var arr = that.data.commentList
              var index = arr.findIndex((item) =>
                item.commentId == commentId
              )
              // console.log("评论index为",index)
              arr.splice(index, 1)
              that.setData({
                commentList: arr
              })
            }
          })
        }
      }
    })
  },
  // 获取初始评论
  init_comments(userToken) {
    // console.log(this.data)
    var that = this
    wx.request({
      url: getComment_url,
      method: "get",
      data: {
        pageNum: 1,
        pageSize: pageSize,
        user_token: userToken
      },
      success(res) {
        console.log(res.data.data.list)
        var totalPage = res.data.data.totalPage
        var code = res.data.code
        var list = res.data.data.list
        var tmplist = []
        if (code == 200) {
          if (list.length != 0) {
            list.forEach(
              function(item, index) {
                tmplist[index] = {}
                var date = new Date(item.timestamp)
                tmplist[index].commentDate = util.formatTime(date)
                tmplist[index].myComment = item.text
                tmplist[index].thumbUpCount = item.stars
                tmplist[index].commentId = item.commentId
              }
            )
            var curComments = tmplist
            that.setData({
              commentList: curComments,
              totalPage: totalPage
            })
          }
        }
      },
      fail(err) {
        wx.showToast({
          title: '网络错误，评论加载失败！',
          icon: 'none'
        })
      }
    })
  },
  //获取更多评论
  load_more(userToken) {
    var that = this
    if (pageNum > that.data.totalPage) {
      wx.showToast({
        title: '没有更多了！',
        icon: 'none'
      }, 300)
    } else {
      wx.showLoading({
        title: "加载中",
      }, 500)
      wx.request({
        url: getComment_url,
        data: {
          pageNum: pageNum,
          pageSize: pageSize,
          user_token: userToken
        },
        success: function (res) {
          wx.hideLoading();
          console.log(res.data.data.list)
          var code = res.data.code
          var list = res.data.data.list
          var tmplist = []
          if (code == 200) {
            if (list.length != 0) {
              console.log(pageNum)
              pageNum++;
              list.forEach(
                function (item, index) {
                  tmplist[index] = {}
                  var date = new Date(item.timestamp)
                  tmplist[index].commentDate = util.formatTime(date)
                  tmplist[index].myComment = item.text
                  tmplist[index].thumbUpCount = item.stars
                  tmplist[index].commentId = item.commentId
                }
              )
              var originComments = that.data.commentList
              var curComments = originComments.concat(tmplist)
              that.setData({
                commentList: curComments

              })
            }
          
          }

        }
      });
    }
  },
  //触底拉取更多评论
  onReachBottom(e) {
    var userToken = this.data.userToken
    this.load_more(userToken)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      userName: app.globalData.userName,
      userToken: app.globalData.userToken
    })
    this.init_comments(app.globalData.userToken)
  },


})